package com.cq.hd.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cq.hd.member.mapper.TbUserBusinessMapper;
import com.cq.hd.member.po.TbUserBusinessPo;
import com.cq.hd.member.service.TbUserBusinessService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户-商家关联表 服务实现类
 * </p>
 */
@Service
public class TbUserBusinessServiceImpl extends ServiceImpl<TbUserBusinessMapper, TbUserBusinessPo> implements TbUserBusinessService {

    // 根据用户id获取商家id列表
    @Override
    public List<Long> getBusinessIdsByUserId(Long userId) {
        return baseMapper.getBusinessIdsByUserId(userId);
    }

    // 用户跟商家关联
    @Override
    public Boolean related(Long userId, Long businessId) {
        Integer count = baseMapper.selectCount(new QueryWrapper<TbUserBusinessPo>()
                .eq("user_id", userId)
                .eq("business_id", businessId));
        if (count != 0) {
            return true;
        }

        try {
            TbUserBusinessPo userBusinessPo = new TbUserBusinessPo();
            userBusinessPo.setUserId(userId);
            userBusinessPo.setBusinessId(businessId);
            userBusinessPo.setCreateTime(LocalDateTime.now());
            return save(userBusinessPo);
        } catch (Exception e) {
            return false;
        }
    }
}
